// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import DBShadowModel from "./DBShadowModel";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Main extends cc.Component {
    shadowLayer: cc.Node;
    playerNode: cc.Node;
    armatureDisplay: dragonBones.ArmatureDisplay;
    speed: number;
    spCd: number;
    aniNames: string[];
    aniIdx: number = -1;

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        console.log("Main")
        this.shadowLayer = cc.find('shadowLayer', this.node);
        this.playerNode = cc.find('player', this.node);
        this.armatureDisplay = this.playerNode.getComponent(dragonBones.ArmatureDisplay);
        this.aniNames = this.armatureDisplay.getAnimationNames('armatureName');
    }

    start() {

    }

    go() {
        this.armatureDisplay.playAnimation(this.aniNames[(this.aniIdx++) % this.aniNames.length], 1);
        this.speed = (this.aniIdx % 2) == 0 ? 1000 : -1000;
        this.playerNode.scaleX = this.speed < 0 ? -1 : 1;
        this.spCd = 0.3;
        let armature: dragonBones.Armature = this.armatureDisplay.armature()
        let animation = armature.animation;
        let dur = animation.animations[animation.lastAnimationName].duration;
        dur = Math.max(dur,0.5)
        DBShadowModel.instance.makeShadow(this.playerNode, dur, 1, this.shadowLayer, 175,-175);
    }

    update(dt) {
        if (this.spCd > 0) {
            this.spCd -= dt;
            if (this.spCd <= 0) {
                this.speed = 0;
            }
        }
        if (this.speed) {
            this.playerNode.x += this.speed * dt;
        }
    }
}
